//
//  AddWalletController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/7.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "AddWalletController.h"
#import "UIButton+BCYImage.h"
#import "FbeeCommitBtn.h"
#import "ConfirmWalletController.h"
#import "FormatChecker.h"

@interface AddWalletController ()
{
    UIScrollView *mainView;
    AddBeeGroupType walletType;
    UIButton *closeBtn;
    UILabel *infoL;
    UITextField *inputF;
    FbeeCommitBtn *commit;
}

@end

@implementation AddWalletController

-(void)dealloc
{
    NSLog(@"AddWalletController dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

-(instancetype)initWithType:(AddBeeGroupType)type
{
    self = [super init];
    
    if (self) {
        walletType = type;
    }
    
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self hiddeBack];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close) name:Notification_AddWalletClose object:nil];
    
    [self buildView];
    
}

-(void)buildView
{
    mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT)];
    mainView.backgroundColor = UIColorFromRGBA(0x000000, 0.5);
    [self.view addSubview:mainView];
    
    NSString *info =@"";
    NSString *navTile = @"";
    NSString *btnTitle = @"";
    if (walletType == BeeAddType) {
        info = NSLocalizedString(@"textAddWalletAdd", nil);
        navTile = NSLocalizedString(@"tNavUCenterWalletAdd", nil);
        btnTitle = NSLocalizedString(@"mAdd", nil);
    }
    
    
    
    if (walletType == MyAddType) {
        info = NSLocalizedString(@"textAddWalletAdd", nil);
        navTile = NSLocalizedString(@"uCenterAddWallet", nil);
        btnTitle = NSLocalizedString(@"uCenterAddWallet", nil);
        
    }
    
    
    
    infoL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addTextColor(COLOR_WHITE).addFont([UIFont systemFontOfSize:14.0f]);
        make.addText(info);
        make.setFrame(CGRectMake(17, self.largeTitle.bottom, IPHONEWIDTH - 17*2, 20));
        [self.view addSubview:make];
    }];
    
    //输入框
    {
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:NSLocalizedString(@"textWalletAddrPlaceholder", nil)];
        text.yy_color =COLOR_WHITE;
        inputF = [[UITextField alloc] initWithFrame:CGRectMake(30, self.largeTitle.bottom + 80, IPHONEWIDTH - 30*2, 40)];
        inputF.font = [UIFont systemFontOfSize:14.0f];
        inputF.backgroundColor = [UIColor clearColor];
        inputF.textColor = COLOR_WHITE;
        inputF.autocapitalizationType = UITextAutocapitalizationTypeNone;
        inputF.attributedPlaceholder = text;
        inputF.textAlignment = NSTextAlignmentCenter;
        inputF.keyboardType = UIKeyboardTypeASCIICapable;
        inputF.autocorrectionType = UITextAutocorrectionTypeNo;
        inputF.clearButtonMode = UITextFieldViewModeAlways;
        inputF.tintColor = FBEE_COLOR_ASSISTANT;
        [mainView addSubview:inputF];
    }
    
    commit = [[FbeeCommitBtn alloc] initWithTitle:btnTitle];
    commit.centerX = IPHONEWIDTH/2;
    commit.top = inputF.bottom + 65;
    @weakify(self)
    [commit onClick:^{
        @strongify(self)
        [self toConfirm];
    }];
    [mainView addSubview:commit];
    
    @weakify(inputF)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        @strongify(inputF)
        [inputF resignFirstResponder];
    }];
    [mainView addGestureRecognizer:tap];
    
    [self.largeTitle setTitle:navTile];
    [self.largeTitle setTitleColor:COLOR_WHITE];
    
    closeBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 44, 44));
        make.addImage([UIImage imageNamed:@"nav_close_w"],UIControlStateNormal);
        make.addTarget(self,@selector(close));
        [self.largeTitle addSubview:make];
    }];
    [closeBtn setCenterByImageSize:(CGSizeMake(17, 17))];
    
    [self bringTitleToFront];
}

-(void)toConfirm
{
    if ([NSString isEmptyString:inputF.text] || ![FormatChecker validateWalletAddr:inputF.text]) {
        [NoticePopHelper showNoticeText:NSLocalizedString(@"textEmptyWalletAddr", nil)];
        return;
    }
    
    ConfirmWalletController *vc = [[ConfirmWalletController alloc] initWithAddr:inputF.text];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)close
{
    [inputF resignFirstResponder];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([KeyBoardManager sharedManager].animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    });
}

@end
